//
//  MessageInformationViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/10/23.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "MessageInformationViewController.h"
#import "MessageInformationTableViewCell.h"
#import "MessageViewController.h"
#import "WeChatLoginViewController.h"
@interface MessageInformationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * cellArr;
@property (nonatomic, strong)NSMutableArray * messageNumCell;
@property (nonatomic, strong)NSString * syscount;//系统消息
@property (nonatomic, strong)NSString * listcount;//订单消息
@property (nonatomic, strong)NSString * phoneString;
@property (nonatomic, strong)NSString * tokenString;
- (void)ifLogin;
@end

@implementation MessageInformationViewController(networking)
- (void)getMessageNumData{
    NSDictionary * dic = @{
                           @"phone":self.phoneString,
                           @"token":self.tokenString,
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"messageiosApi/messcount" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            if ([responseObject[@"list"][@"syscount"] isEqualToString:@""] || [responseObject[@"list"][@"syscount"] length] == 0 || responseObject[@"list"][@"syscount"] == nil) {
                self.syscount = @"0";
            }else{
                self.syscount = responseObject[@"list"][@"syscount"];
            }
            if ([responseObject[@"list"][@"listcount"] isEqualToString:@""] || [responseObject[@"list"][@"listcount"] length] == 0 || responseObject[@"list"][@"listcount"] == nil) {
                self.listcount = @"0";
            }else{
                self.listcount = responseObject[@"list"][@"listcount"];
            }
            [self.tableView reloadData];
        }else if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"400"]) {
            [self ifLogin];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation MessageInformationViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    if ([[UserModel sharedInstanced].token isEqualToString:@""] || [UserModel sharedInstanced].token == nil || [[UserModel sharedInstanced].token length] == 0 || [[UserModel sharedInstanced].mobile isEqualToString:@""] || [UserModel sharedInstanced].mobile == nil || [[UserModel sharedInstanced].mobile length] == 0) {
        self.tokenString = @"";
        self.phoneString = @"";
    }else{
        self.tokenString = [UserModel sharedInstanced].token;
        self.phoneString = [UserModel sharedInstanced].mobile;
    }
    [self getMessageNumData];//网络接口
    self.listcount = @"0";
    self.syscount = @"0";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"消息通知";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
}
#pragma mark - init
- (NSMutableArray *)cellArr{
    if (!_cellArr) {
        _cellArr = @[
                     @{@"title":@"订单消息", @"img":@"trumpet"},
                     @{@"title":@"系统消息", @"img":@"message"}
                     ].mutableCopy;
    }return _cellArr;
}
- (NSMutableArray *)messageNumCell{
    if (!_messageNumCell) {
        _messageNumCell = @[].mutableCopy;
    }return _messageNumCell;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 100/WIDTH_6_SCALE;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        [_tableView registerClass:[MessageInformationTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MessageInformationTableViewCell class])];
        _tableView.backgroundColor = getColor(mainColor);
    }return _tableView;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageInformationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MessageInformationTableViewCell class])];
    cell.regularModel = self.cellArr[indexPath.row];
    if (indexPath.row == 0) {
        if ([self.listcount isEqualToString:@"0"]) {
            cell.redCircleImag.hidden = YES;
            cell.numLab.hidden = YES;
        }else{
            cell.redCircleImag.hidden = NO;
            cell.numLab.hidden = NO;
            cell.numLab.text = self.listcount;
        }
    }
    if (indexPath.row == 1) {
        if ([self.syscount isEqualToString:@"0"]) {
            cell.redCircleImag.hidden = YES;
            cell.numLab.hidden = YES;
        }else{
            cell.redCircleImag.hidden = NO;
            cell.numLab.hidden = NO;
            cell.numLab.text = self.syscount;
        }
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageViewController * vc = [[MessageViewController alloc] init];
    if (indexPath.row == 0) {
        vc.messageType = @"order";
    }else{
        vc.messageType = @"system";
    }
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - action
- (void)ifLogin{
    WeChatLoginViewController * vc = [[WeChatLoginViewController alloc] init];
    UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.backHomePageBlock = ^{
        [vc dismissViewControllerAnimated:YES completion:nil];
        [self.tabBarController setSelectedIndex:0];
        [self.navigationController popToRootViewControllerAnimated:YES];
    };
    [self presentViewController:navi animated:YES completion:nil];
}
@end
